<?php
/**
 * 权限管理
 */
namespace Admin\Controller;
use Admin\Controller\AdminController;
use Common\Helper\Category;
class PowerController extends AdminController {
	
	/**
	 * 权限管理
	 */
	
	///权限列表
	public function powerindex(){
		$category_list = M('Auth_rule')->select();
		$category_list = Category::toLevel($category_list,'——');
		$this->assign('category_list', $category_list);
		$this->display();
	}
	
	///增加 、修改权限
	 public function insertpower(){
	   $data['parent_id'] = I('parent_id')!==''?I('parent_id'):null;
	   $data['title'] = I('title') !==''?I('title'):null;
	   $data['name'] = I('name') !==''?I('name'):null;
	   $id = I('id')!==''?I('id'):null;
	   $note = [
	             'parent_id'=>'父权限',
	             'title'=>'权限名',
	             'name'=>'权限',
	           ];
	   $bool = $this -> checkField($data, $note);
	   if($bool){
	        if(empty($id)){
	        	$name = $data['name'];
	        	if( M("Auth_rule")->where("name = '$name'")->find() ){
	        		$this ->error("权限重复");
	        	}

    	        if ( M('Auth_rule')->data($data)->add() ){
    	            $this -> success('添加成功');
    	        }else{
    	        	$this -> error('添加失败');
    	        }
    	    }else{

    	        if( M('Auth_rule')->where("id = '$id'")->save($data) ){
    	            $this -> success('更新成功');
    	        }else{
    	        	$this -> error('更新失败');
    	        }
    	    }
	   }
	 }
	 
	 ////删除权限
     public function delpower(){
        $id = I('id',null);
        $note = [
                     'id' => '栏目编号',
                ];
        $bool = $this -> checkField($where, $note);
        if($bool){
           if( M("Auth_rule")->where("parent_id = '$id'")->select() ){
               $this -> error('不能删除该权限!#该栏目存在子权限#');
           }
           if(M('Auth_rule')->where("id = '$id'")->delete()){
              $this -> success('删除成功'); 
            }else{
            	$this -> success('删除失败');
            }
        }
    }
    
    ////更改请求数据
    public function update(){
    	$id = I('id',null);
    	$where['id'] = $id;
    	$note = [
    	'id' => '栏目编号',
    	];
    	$bool = $this -> checkField($where, $note);
    	if($bool){
    		$res = M('Auth_rule')->where($where)->find();
    		$data['status'] = 1;
    		$data['arr']  = $res;
    		$this -> ajaxReturn($data);
    	}else{
    		$data['info'] = '数据不正确';
    		$data['status'] = -1;
    		$this -> ajaxReturn($data);
    	}
    }
    
    
    /**
     * 权限组设置
     */
    
    ////权限组列表
    public function groupindex(){
    
       $res = M('Auth_group')->select(); 
       foreach($res as $k=>$v){
          ///获取人数
          $num = M('Auth_access')->where("group_id = '".$v['id']."'")->count();
          $res[$k]['num'] = $num;
          ///获取权限名称
          $rulesArr = explode(',',$v['rules']);
          foreach($rulesArr as $kk=>$vv){
              $rulseName[] = M('Auth_rule')->where(" id = '$vv'")->getField('title');
              $rulseStr = implode(',', $rulseName);
          }
          $res[$k]['rulesname'] = $rulseStr;
          unset($rulseName);
       }
       $this -> assign('list',$res);
       $this -> display();
    }
    
    ////增加权限组页
    public function addgroup() {
    	$id = I('id')!==''?I('id'):null;
    	if(!empty($id)){
    		$res = M('Auth_group')->where("id = '$id'")->find();
    		$this -> assign('res',$res);
    	}
        $category_list = M('Auth_rule')->select();
        $category_list = Category::toLevel($category_list,'|—');
    	$this->assign('category_list', $category_list);
    	$this ->display();
    }
    
    ////增权限组操作
    public function insertgroup(){
        $data['title'] = I('title',null);
        $data['status'] = I('status');
        $data['rules'] = I('rules')==''?'':implode(',',I('rules'));
        $id = I('id')!==''?I('id'):null;
        $note = [
                     'title' =>'组名称',
                     'status'=>'状态',
                ];
       $exclude = array('rules');
       $bool = $this -> checkField($data, $note,$exclude);
       if($bool){
       	  if(empty($id)){
       	  	if(M('Auth_group')->data($data)->add()){
       	  		$this -> success('添加成功','groupindex');
       	  	}else{
       	  		$this -> error('添加失败');
       	  	}
       	  }else{

       	  	if(M('Auth_group')->where("id = '$id'")->save($data)){
       	  		$this -> success('修改成功','groupindex');
       	  	}else{
       	  		$this -> error('修改失败');
       	  	}
       	  }
           
       }
    }
    
    ///删除权限组
    public function del(){
    	$id = I('id')!==''?I('id'):null;
    	if(empty($id)){
    		$this -> error('信息错误#编号丢失#');
    	}
    	$res = M('Auth_access')->where("group_id = '$id'")->select();
    	if(!empty($res)){
    		$this -> error('该分组存在成员，无法删除');
    	}
    	if(  M('Auth_group')->where("id = '$id'")->delete() ){
    		$this -> success("删除成功");
    	}else{
    		$this -> error("删除失败");
    	}
    }
    
    
    /**
     * 组成员管理
     */
    
    ///组成员列表
    public function memberindex(){
    	$userArr = M('Admin')->select();
    	foreach($userArr as $k=>$v){
    		$uid = $v['id'];
    		$groupid = M("Auth_access")->where("uid = '$uid'")->getField('group_id');
    		$groupinfo = M('Auth_group')->where("id = '$groupid'")->find();
    		$userArr[$k]['groupname'] = $groupinfo['title'];
    		$userArr[$k]['groupid'] = $groupinfo['id'];
    	}
    	//dump($userArr);
    	$this -> assign('list',$userArr);
    	$this -> display();
    }
    
    ////添加组成员页面
    public function addmember(){
    	$id = I('id')!==''?I('id'):null;
    	if(!empty($id)){
    		$user = M("Admin")->where("id = '$id'")->find();
    		$groupid= M("Auth_access")->where("uid = '$id'")->getField('group_id');
    		$user['groupid'] = $groupid;
    		$this -> assign('info',$user);
    	}
    	$group = M("Auth_group")->select();
    	$this -> assign('list',$group);
    	$this -> display();
    }
    
    ////添加组成员
    public function insertmember(){
    	$data['username'] = I('username')!==''?I('username'):null;
    	$data['password'] = I('password')!==''?md5(I('password')):'';
    	$data['email']    = I('email')!==''?I('email'):null;
    	$data['phone']    = I('phone')!==''?I('phone'):null;
    	$data['status']   = I('status')!==''?I('status'):null;
    	$id               = I('id')?I('id'):null;
    	$note = [
			    	'username'=>'用户名',
			    	'password'=>'密码',
			    	'email'   =>'电子邮箱',
			    	'phone'   =>'电话',
			    	'status'  =>'状态',
    	        ];
    	if(!empty($id) && $data['password'] === ''){
    		unset($note['password']);
    		unset($data['password']);
    	}
    	$bool = $this -> checkField($data, $note);
    	$arr['group_id'] = I('gid')!==''?I('gid'):null;
    	if($arr['group_id'] === null){
    		$this -> error('分组不能为空');
    	}
    	if($bool){
    		if(empty($id)){
    			$data['time'] = time();
    			$uid = M('Admin')->data($data)->add();
    			if($uid){
    				$arr['uid'] = $uid;
    				M('Auth_access')->data($arr)->add();
    				$this -> success('添加成功',"memberindex");
    			}else{
    				$this -> error("添加失败！#用户出错#");
    		    }
    	   }else{
    	   	   M('Admin')->where("id = '$id'")->save($data); 
    	   	   if(M("Auth_access")->where("uid = '$id'")->find()){
    	   	   	 M("Auth_access")->where("uid = '$id'")->setField('group_id',$arr['group_id']);
    	   	   }else{
    	   	   	 M("Auth_access")->data(array("uid"=>$id,"group_id"=>$arr['group_id']))->add();
    	   	   }
    	   	    
    	   	   $this -> success("更新成功","memberindex"); 
    	   }
    	}
    }
    
    ///删除用户
    public function delmember(){
    	$id = I('id')!==''?I('id'):null;
    	if(empty($id)){
    		$this ->error("用户信息错误");
    	}
    	M("Auth_access")->where("uid = '$id'")->delete();
    	M("Admin")->where("id = '$id'")->delete();
    	$this -> success("删除成功");
    }
}